import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class semaphore {

    private static final int threadCount = 100;

    public static void main(String[] args) throws InterruptedException{

        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                50, 100, 1L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.AbortPolicy()
        );

        // 允许多线程访问
        final Semaphore semaphore = new Semaphore(50);

        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            pool.execute(() -> {
                try {
                    semaphore.acquire();
                    test(threadNum);
                    semaphore.release();
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }



        pool.shutdown();
        System.out.println("finished!");
    }

    public static void test (int threadNum) throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("ThreadNUm: " + threadNum);
        Thread.sleep(1000);
    }

}
